import java.util.Scanner;
public class PasswordManager {//加密操作
    public static String encrypt(String password){
        StringBuilder encryptedPassword = new StringBuilder();                  //创建stringbuilder对象用于存储加密后的密码
        int deviant = 3;                                                        //偏移值为3
        for (int i = 0; i< password.length(); i++){                             //遍历输入密码的每一个字符
           char currentChar = password.charAt(i);                               //获取输入的字符
           int asciiValue = (int) currentChar + i + deviant;                    //计算阿斯卡值并与其索引和偏移值相加
           encryptedPassword.append((char)asciiValue);                          //将计算后的值转为字符并附加到stringbuilder中
        }
        return swapFirstAndLastCharacter(encryptedPassword.toString());         //返回并调用私有方法，交换首尾
    }
    public static String decrypt(String encryptedPassword){//解密操作
        String swappedPassword = swapFirstAndLastCharacter(encryptedPassword);  //调用私有方法，交换首尾
        StringBuilder decryptedPassword = new StringBuilder();                  //创建stringbuilder对象用于存储解密后的密码
        int deviant = 3;
        for (int i = 0; i< swappedPassword.length();i++){                       //遍历交换位置后的加密密码的每个字符
            char currentChar = swappedPassword.charAt(i);                       //获取字符
            int asciiValue = (int)currentChar - i - deviant;                    //计算阿斯卡值并减去索引和偏移值
            decryptedPassword.append((char) asciiValue);
        }
        return decryptedPassword.toString();                                    //返回最终解密的密码
    }
    private static String swapFirstAndLastCharacter(String password){           //将字符串的第一位和最后一位调换顺序
        if (password.length() <=1){
            return password;                                                    //判断输入的密码，长度小于1则无需交换
        }
        char[] charArray = password.toCharArray();                              //将字符串转换为字符数组
        char firstChar = charArray[0];
        char lastChar = charArray[charArray.length - 1];
        charArray[0] = lastChar;
        charArray[charArray.length - 1] = firstChar;                            //交换第一个字符和最后一个字符的位置
        return new String(charArray);
    }

    //判断密码强度
    public static String checkStrength(String password) {
        if(password.matches(".*\\d.*") && password.matches(".*[A-Z].*") && password.matches(".*[a-z].*]") && password.length() >=8){
            return "Strong";
        } else if (password.matches(".*\\d.*") && (password.matches(".*[A-Z].*") || password.matches(".*[a-z].*")) && password.length() >= 6) {
            return "Medium";
        }else {
            return "Weak";
        }
    }

    //生成密码
    public static String generatePassword(int length){
        String characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; //定义包含数字、大写字母、小写字母的字符集
        StringBuilder password = new StringBuilder();                                         //创建stringbuilder对象，用于存储生成的密码
        for (int i = 0; i< length; i++){
            int randomIndex = (int) (Math.random() *characters.length());
            password.append(characters.charAt(randomIndex));
        }
        return password.toString();                                                           //返回生成的密码
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.println("        ==================================");
            System.out.println("               欢迎使用密码管理系统");
            System.out.println("        ==================================");
            System.out.println("                   请选择加密操作:");
            System.out.println("               1.加密");
            System.out.println("               2.解密");
            System.out.println("               3.判断密码强度");
            System.out.println("               4.生成密码");
            System.out.println("               5.退出");
            System.out.println();
            System.out.print("请输入选项序号:");

            int choice = scanner.nextInt();
            scanner.nextLine();

            switch(choice){
                case 1:
                    System.out.println("请输入要加密的密码");
                    String passwordToEncrypt = scanner.nextLine();
                    String encryptedPassword = encrypt(passwordToEncrypt);
                    System.out.println("加密后的密码:"+encryptedPassword);
                    break;
                case 2:
                    System.out.print("请输入要解密的密码");
                    String passwordToDecrypt = scanner.nextLine();
                    String decryptedPassword = decrypt(passwordToDecrypt);
                    System.out.println("解密后的密码"+decryptedPassword);
                    break;
                case 3:
                    System.out.print("请输入要判断强度的密码：");
                    String passwordToCheck = scanner.nextLine();
                    String passwordStrength = checkStrength(passwordToCheck);
                    System.out.println("密码强度：" + passwordStrength);
                    break;
                case 4:
                    System.out.print("请输入要生成密码的长度：");
                    int passwordLength = scanner.nextInt();
                    scanner.nextLine();
                    String generatedPassword = generatePassword(passwordLength);
                    System.out.println("生成的密码：" + generatedPassword);
                    break;
                case 5:
                    System.out.println("程序已退出。");
                    System.exit(0);
                default:
                    System.out.println("无效的选择，请重新选择。");
            }
        }
    }
}//这是我的仓库.。
//这是一个程序
